package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.GybWeighRecordEXP;
import com.tjsiqi.model.JbzContractEXP;
import com.tjsiqi.param.JbzContractParam;
import com.tjsiqi.param.WeighCountParam;
import com.tjsiqi.service.SysJbzContractService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by lxc
 */

@Controller
@RequestMapping("/sys/JbzContract")
public class SysJbzContractController {

    @Resource
    private SysJbzContractService sysJbzContractService;
//  跳转到合同页面
    @RequestMapping("/Contract.page")
    public ModelAndView JbzContractPage() {
        return new ModelAndView("JbzContract");
    }

//  查询合同集合
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(JbzContractParam jbzContractParam, PageQuery page) {
        return JsonData.success(sysJbzContractService.searchPageList(jbzContractParam, page));
    }
//导出合同集合
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, JbzContractParam jbzContractParam) throws IOException {
        String filename = "合同单" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<JbzContractEXP> util = new ExportExcelUtil<JbzContractEXP>();
        List<JbzContractEXP> JbzContractList = sysJbzContractService.exportByParam(jbzContractParam);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"录入人", "录入日期", "合同编号", "工程名称", "施工单位", "浇筑部位", "工程状态", "现场联系人", "现场电话", "签订时间", "付款方式", "争议解决方式", "其他约定事项", "买受人", "买受人地址", "买受人法人", "买受人代理人", "买受人办公电话", "买受人传真", "买受人邮编", "买受人银行账户", "出卖人", "出卖人地址", "出卖人法人", "出卖人代理人", "出卖人办公电话", "出卖人传真", "出卖人邮编", "出卖人银行账户", "签订地点", "合同类型", "建设单位", "现场手机", "结款状态"};
        util.exportExcel("合同列表", columnNames, JbzContractList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }


}
